/**
 * @title ClearEditText.java
 * @package com.esun.cheetah.view.custom
 * @description 带清除功能的EditText
 * @author liugan
 * @date 2012-4-23
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.view.custom;

import com.esun.cheetah.R;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;

/**
 * @title ClearEditText
 * @description 带清除功能的EditText
 * @author liugan
 */
public class ClearEditText extends EditText {

	/**
	 * 外部注册的TextWatcher 因为内部已经用到了TextWatcher
	 */
	private TextWatcher mExternalWatcher;

	/** 输入框右边的删除图标 */
	private Drawable mRightDrawable;

	/**
	 * @param context
	 * @param attrs
	 */
	public ClearEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ClearEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public ClearEditText(Context context) {
		super(context);
		init();
	}

	private void init() {
		mRightDrawable = getResources()
				.getDrawable(R.drawable.txt_search_clear);
		this.addTextChangedListener(new InternelTextWatcher());
	}

	private void showClear() {
		this.setCompoundDrawablesWithIntrinsicBounds(null, null,
				mRightDrawable, null);
	}

	private void hiddenClear() {
		this.setCompoundDrawables(null, null, null, null);
	}

	@Override
	public void addTextChangedListener(TextWatcher watcher) {
		if (watcher instanceof InternelTextWatcher) {
			super.addTextChangedListener(watcher);
		} else {
			mExternalWatcher = watcher;
		}

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_UP:

			if (!TextUtils.isEmpty(this.getText())) {
				// 删除图标的柱形区域
				final Rect deleteRect = new Rect();
				deleteRect.top = 0;
				deleteRect.bottom = this.getHeight();
				deleteRect.right = this.getWidth();
				deleteRect.left = deleteRect.right
						- this.getCompoundPaddingRight();

				if (deleteRect.contains((int) event.getX(), (int) event.getY())) {
					this.setText(null);
					// 保存InputType状态
					int cacheInputType = this.getInputType();
					// 禁止软键盘
					this.setInputType(InputType.TYPE_NULL);
					super.onTouchEvent(event);
					// 还原InputType状态
					this.setInputType(cacheInputType);
					return true;// consume touch even

				}
			}

			break;
		}
		return super.onTouchEvent(event);
	}

	private class InternelTextWatcher implements TextWatcher {

		// 缓存上一次文本框内是否为空
		private boolean isNull = true;

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			if (mExternalWatcher != null) {
				mExternalWatcher.beforeTextChanged(s, start, count, after);
			}
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			if (mExternalWatcher != null) {
				mExternalWatcher.onTextChanged(s, start, before, count);
			}
		}

		@Override
		public void afterTextChanged(Editable s) {
			if (!TextUtils.isEmpty(s)) {
				if (isNull) {
					ClearEditText.this.showClear();
					isNull = false;
				}
			} else {
				ClearEditText.this.hiddenClear();
				isNull = true;
			}
			if (mExternalWatcher != null) {
				mExternalWatcher.afterTextChanged(s);
			}

		}

	}

}
